<template xmlns="http://www.w3.org/1999/html">
  <div class="app-container">

    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="活动名称" prop="promotionName">
            <el-input v-model="form.promotionName" placeholder="请输入活动名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报名截止时间" >
            <el-date-picker
              v-model="form.applyEndTime"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetime"
              placeholder="选择报名截止时间"
              default-time="00:00:00">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>


      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="活动开始日期" prop="startTime">
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="form.startTime"
              type="date"
              placeholder="选择活动开始日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="24">
          <el-form-item label="申请规则" prop="seckillRule">
            <el-input type="textarea"  :rows="4"   maxlength="100" show-word-limit v-model="form.seckillRule" placeholder="请输入申请规则" />
          </el-form-item>
        </el-col>
      </el-row>


      <el-row :gutter="30">
        <el-col :span="24">

          <el-form-item label="抢购时间段" >
            <el-tag
              :key="tag.time"
              v-for="tag in hours"
              closable
              :disable-transitions="false"
              @close="handleClose(tag)"
              style="margin-right: 10px">
              {{tag.time}}
            </el-tag>
            <el-time-select
              v-if="inputVisible"
              v-model="inputValue.time"
              :picker-options="{
                start: '00:00',
                step: '00:10',
                end: '23:50'
              }"
              placeholder="选择时间"
              @keyup.enter.native="handleInputConfirm"
              @blur="handleInputConfirm"
            >
            </el-time-select>
            <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新增</el-button>
          </el-form-item>




        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { addSeckill, getSeckill, updateSeckill} from "@/api/mall/promotion/seckill";
import { getSettingByKey } from '@/api/sysSet/setting'
export default {
  name: "MallSeckillAdd",
  props: {
    seckillId: {
      type: String
    }
  },
  data() {
    return {
      configKey: 'seckill_config',
      // 遮罩层
      loading: true,
      inputVisible: false,
      // 表单参数
      form: {
        promotionName: '',
        applyEndTime: '',
        startTime: '',
        seckillRule: '',
        hours: '',
      },
      // 表单校验
      rules: {
        promotionName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 2, max: 20, message: '活动名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        requiredNum: [
          { required: true, message: "请输入参团人数", trigger: "blur" },
        ],
        limitNum: [
          { required: true, message: "请输入限购数量", trigger: "blur" },
        ],
        rangeTime: [{ required: true, message: "请选择优惠券有效期" ,trigger: "blur"}],
      },
      id: null,
      inputValue: {},
      hours: []
    };
  },
  created() {

    if (!this.seckillId){
      this.getSeckillConfig();
    }else{
      this._getSeckill()
    }


  },
  methods: {
    /** 查询参数配置列表 */
    getSeckillConfig() {
      getSettingByKey(this.configKey).then(response => {
        let configValue = response.data.configValue
        this.hours = JSON.parse(configValue).hours
      })
    },
   /** 查询秒杀信息 */
   _getSeckill() {
     getSeckill(this.seckillId).then(response => {
          this.form = response.data
          let hours = this.form.hours;
          this.hours = []
          if (hours && hours.length > 0){
            let hourArr = this.form.hours.split(",");
            for (let i = 0;i<hourArr.length;i++){
              let hour = hourArr[i];
              let item = {
                "time": hour
              }
              this.hours.push(item)
            }

          }
        })
      },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.hours.length < 1){
            this.$modal.msgError("请设置抢购时间段");
            return;
          }
          let pureHourr = this.hours.map(obj => obj.time);
          this.form.hours = pureHourr.join(",")



          if (this.form.id != null) {
            updateSeckill(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$emit("emitSeckillAdd");
            });
          } else {
            addSeckill(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$emit("emitSeckillAdd");
            });
          }

        }
      });
    },

    cancel(){
      this.$emit("emitSeckillCancel");
    },

    showInput() {
      this.inputVisible = true;
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.hours.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = {};
    },
    handleClose(tag) {
      this.hours.splice(this.hours.indexOf(tag), 1);
    },

  }
};
</script>
